﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace myScheduler
{
    public partial class Courses : System.Web.UI.Page
    {
        SQLcontrol SQLcontroller = new SQLcontrol();

        protected void Page_Load(object sender, EventArgs e)
        {
            //lblCourses.Text = loadCourses();
        }

        protected void btnSearch_onClick(object sender, EventArgs e)
        {
            lblCourses.Text = loadCourses();
        }

        string loadCourses()
        {
            string result = "";
            try
            {
                //Connect to the database
                string sql = SQLcontroller.ReadSQLscript("loadCourses.sql");
                sql = sql.Replace("$CODE", txtCourseCode.Text);
                sql = sql.Replace("$NUMBER", txtCourseNumber.Text);
                if (radSemester.SelectedValue == "all")
                {
                    sql = sql.Replace("$SEMESTER", ""); //replace with emply string
                }
                else
                {
                    sql = sql.Replace("$SEMESTER", "AND season='" + radSemester.SelectedValue + "'");   //inject the semester condition into the query
                }

                //Update notification accordingly
                if (txtCourseCode.Text == "" && txtCourseNumber.Text == "")
                {
                    lblSearchNotice.Text = "Searching for all courses";
                }
                else
                {
                    lblSearchNotice.Text = "Searching for " + txtCourseCode.Text.ToUpper() + " " + txtCourseNumber.Text.ToUpper();
                }
                
                DataTable SQLdata = new DataTable();
                SQLdata = SQLcontroller.SQLread(sql).Tables[0];

                //Create table
                result = "<table cellpadding='5' cellspacing='0'><tr valign='top'><td> <table>";
                string tempCode = "";
                foreach (DataRow drRow in SQLdata.Rows)
                {
                    //load temporary course code for layout purposes
                    if (tempCode != drRow[0].ToString())
                    {
                        tempCode = drRow[0].ToString();
                        result += "</table> </td><td style='border-right: solid 0px;'> <table>"; //Create nested table to have multi-column layout for different course code
                    }

                    //Output data
                    result += "<tr>";
                    result += "<td><a target='_blank' href='http://fcms.concordia.ca/fcms/asc002_stud_all.aspx?yrsess=20123&course=" + tempCode + "&courno=" + drRow[1].ToString() + "'>";
                    result += tempCode + drRow[1].ToString();
                    result += "(" + getSeason(drRow[2].ToString()) + ")</a></td>";
                    result += "</tr>";
                }
                result += "</table></td></tr></table>";
                return result;
            }
            catch
            {
                return "Database is not available. Retry in a few moments.";
            }
        }

        string getSeason(string input)
        {
            if (input == "2")
                return "F";
            else if (input == "3")
                return "F&W";
            else if (input == "4")
                return "W";
            else
                return "TBD";
        }
    }
}